<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>webSocket演示</title>
</head>

<body>
  <h1>webSocket演示</h1>
  <div>
    <input type="text" id="address" value="127.0.0.1:8083">
    <button id="connect">连接</button>
    <span>8083 8084</span>
  </div>
  <div>
    <input type="text" id="msg" disabled>
    <button id="send" disabled>发送</button>
  </div>
  <textarea cols="40" rows="10" readonly id="log"></textarea>
  <script>
    const addressText = document.getElementById('address')
    const connectBtn = document.getElementById('connect')
    const msgText = document.getElementById('msg')
    const sendBtn = document.getElementById('send')
    const logText = document.getElementById('log')

    let status = false;
    let ws;

    connectBtn.addEventListener('click', function () {
      if (!status) {
        ws = new WebSocket("ws://" + addressText.value + "/")
        ws.onopen = function () {
          onStart();
          log("连接成功！")
        }
        ws.onmessage = function (e) {
          log("收到消息：" + e.data);
        }
        ws.onclose = function () {
          onEnd();
          log("连接已断开！");
        }
        ws.onerror = function () {
          onEnd();
          log("发生错误！");
        }
      } else {
        ws.close();
        onEnd();
      }
    })

    sendBtn.addEventListener('click', function () {
      ws.send(msg.value);
      log("发送消息：" + msg.value)
    })

    function onStart() {
      status = true;
      addressText.disabled = true;
      connectBtn.innerText = "断开";
      msgText.disabled = false;
      sendBtn.disabled = false;
    }

    function onEnd() {
      status = false;
      addressText.disabled = false;
      connectBtn.innerText = "连接";
      msgText.disabled = true;
      sendBtn.disabled = true;
    }

    function log(msg) {
      logText.value = (msg + '\n' + logText.value);
    }

  </script>
</body>

</html>